package com.yimi.yinhepda.views.setting_module;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pda.ym.base.BaseActivity;
import com.pda.ym.base.BaseTitleActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.downLoadBaseData.DownLoadBaseData;
import com.yimi.yinhepda.downLoadBaseData.DownLoadFile;
import com.pda.ym.utils.ActivityStack;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.uce.commom.util.MyDialog;
import com.yimi.yinhepda.views.login.activity.LoginActivity;

import org.jetbrains.annotations.NotNull;

/**
 * 系统设置界面
 * @author lj
 */
public class SettingActivity extends BaseTitleActivity {

    private Button btnDownLoadApp;

    @Override
    public int setContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.activity_setting_wifi).setOnClickListener(this);
        findViewById(R.id.activity_setting_bluetooth).setOnClickListener(this);
        Button downloadBtn = (Button) findViewById(R.id.activity_setting_download_data);
        downloadBtn.setOnClickListener(this);
        findViewById(R.id.activity_setting_serverip).setOnClickListener(this);
        findViewById(R.id.activity_setting_pdainfo).setOnClickListener(this);
        findViewById(R.id.activity_setting_login_off).setOnClickListener(this);
        findViewById(R.id.activity_setting_back).setOnClickListener(this);
        btnDownLoadApp =  findViewById(R.id.btn_downloadApp);
        btnDownLoadApp.setOnClickListener(this);

        int deptType = sharedPreferUtil.getIntValue(Common.DEPT_TYPE);
        if (deptType == 5 || deptType == 6) {
            btnDownLoadApp.setVisibility(View.GONE);
        } else {
            //网点才使用APP下载
            btnDownLoadApp.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public String getPageName() {
        return "系统设置";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_setting_wifi:
                Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                startActivity(intent);
                break;
            case R.id.activity_setting_bluetooth:
                Intent wifiIntent = new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivity(wifiIntent);
                break;
            case R.id.activity_setting_download_data:
                //基础数据下载
                if (!sharedPreferUtil.getOfflineMode()) {
                    //下载基础数据
                    new DownLoadBaseData(this, true).downloadBaseData();
                } else {
                    showToast("离线模式不可下载数据！");
                }
                break;
            case R.id.activity_setting_serverip:
                toActivity(ServerSettingActivity.class);
                break;
            case R.id.activity_setting_pdainfo:
                Intent pdaInfoIntent = new Intent(SettingActivity.this, PdaInfoActivity.class);
                startActivity(pdaInfoIntent);
                break;
            case R.id.activity_setting_login_off:
                //退出上传离线状态日志
                Common.uploadPdaManagerLogInfo(this, 2);
                ActivityStack.getInstance().exit();
                Intent loginIntent = new Intent(SettingActivity.this, LoginActivity.class);
                loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(loginIntent);
                break;
            case R.id.btn_downloadApp:
                //APP下载
                showDownLoadDialog();
                break;
            case R.id.activity_setting_back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 下载提示
     */
    private void showDownLoadDialog() {
        MyDialog dialog = new MyDialog(this);
        dialog.setIcon(R.drawable.alert);
        dialog.setTitle("提示");
        dialog.setMessage("壹米在手APP支持安卓4.4以上版本使用，否则可能安装失败，确定下载？");
        dialog.setPositiveClickListener( Constants.confirmMsg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                DownLoadFile.getInstance(SettingActivity.this).downloadAPP();
            }
        });
        dialog.setNegativeClickListener(R.drawable.btn_cancel_selector, Constants.cancelMsg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return getPageName();
    }

}
